package com.yyys.web.service.impl;

import com.yyys.web.entity.DRInfo;
import com.yyys.web.repository.DRInfoRepository;
import com.yyys.web.service.DRInfoService;
import com.yyys.web.utils.Constant;
import com.yyys.web.utils.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by zhengsenwen on 2017/7/31.
 */

@Service
public class DRInfoServiceImpl implements DRInfoService {

    private Logger logger = Logger.getLogger(DRInfoServiceImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DRInfoRepository drInfoRepository;

    @Override
    public List<DRInfo> getAll() throws Exception {
        return drInfoRepository.findAll();
    }


    @Override
    public DRInfo getDrInfoById(Integer id) throws Exception {
        return drInfoRepository.findById(id);

    }

    @Override
    public String getRecommendedMealTime(Integer drId) {
        String bestMealTime = "";
        List list = new ArrayList<>();
        String today = DateUtils.getCurrentDay();
        String sevenDaysBeforeToday = DateUtils.getLastDay(-7);
        String startTime = DateUtils.getMealTime().substring(0, 8);
        String endTime = DateUtils.getMealTime().substring(9, 17);

        logger.info("getRecommendedMealTime:drId: "
                + drId + " today:" + today + " sevenDaysBeforeToday: " + sevenDaysBeforeToday
                + " startTime: " + startTime + " endTime: " + endTime);
        String statement =
                "SELECT  \n" +
                        "\tTOP 1\n" +
                        "\tDRID,INOROUT," +
                        "\t--,CONVERT(VARCHAR(10),OCCUTIME,120) TDATE\n" +
                        "RIGHT('00'+CAST(DATEPART(HH,OCCUTIME) AS VARCHAR),2) + ':' + \n" +
                        "\tRIGHT('00'+CAST(( DATEPART(MI,OCCUTIME) / 30) * 30 AS VARCHAR),2) TTIME\n" +
                        "\t,2 * DATEPART(HH,OCCUTIME) + DATEPART(MI,OCCUTIME) / 30 TINDEX \n" +
                        "\t,COUNT(1) INOROUTCOUNT \n" +
                        "FROM \n" +
                        "\t [FLOW_DRTransInfo]\n" +
                        "WHERE \n" +
                        "\tDRID = '" + drId + "' \n" +
                        "\tAND CONVERT(varchar(10),OccuTime,120) >= '" + sevenDaysBeforeToday + "' \n" +
                        "\tAND CONVERT(varchar(10),OccuTime,120) < '" + today + "' \n" +
                        "\tAND DATEPART(WEEKDAY,CONVERT(varchar(10),OccuTime,120)) NOT IN (1,7)\n" +
                        "\tAND InOrOut = '" + Constant.ENTER_RESTUARANT_TAG + "'\n" +
                        "\tAND SUBSTRING(CONVERT(varchar(20),OccuTime,120),12,8) >= '" + startTime + "'\n" +
                        "\tAND SUBSTRING(CONVERT(varchar(20),OccuTime,120),12,8) < '" + endTime + "'\t\n" +
                        "GROUP BY \n" +
                        "\tDRID,INOROUT," +
                        "\t--,CONVERT(VARCHAR(10),OCCUTIME,120)\n" +
                        "RIGHT('00'+CAST(DATEPART(HH,OCCUTIME) AS VARCHAR),2) + ':' + \n" +
                        "RIGHT('00'+CAST(( DATEPART(MI,OCCUTIME) / 30) * 30 AS VARCHAR),2) \n" +
                        "\t,2 * DATEPART(HH,OCCUTIME) + DATEPART(MI,OCCUTIME) / 30\n" +
                        "ORDER BY \n" +
                        "COUNT(1)";

        list = jdbcTemplate.queryForList(statement);
        logger.info("getDailyRecommend: list is null and dailyRecommend is null");


        if (list != null && list.size() > 0) {
            Iterator iterator = list.iterator();
            while (iterator.hasNext()) {
                Map tempMap = (Map) iterator.next();
                bestMealTime = (String) tempMap.get("TTIME");

                //拼接推荐就餐时间  例如，返回18:30，拼接成18:30 — 19:00
                Integer startHour = Integer.valueOf(bestMealTime.substring(0, 2));
                Integer endHour = null;
                String startMin = bestMealTime.substring(3, 5);
                String endMin = null;
                if (startMin.equals("00")) {
                    endMin = "30";
                    endHour = startHour;
                } else if (startMin.equals("30")) {
                    endHour = startHour + 1;
                    endMin = "00";
                }
                bestMealTime = String.valueOf(startHour) + ":" + startMin
                        + " — " + String.valueOf(endHour) + ":" + endMin;
            }
        }

        return bestMealTime;
    }


}
